package mfuny.ggongal.admin.http;

import mfuny.ggongal.admin.debug.Debug;
import mfuny.ggongal.admin.https.GGDecoder;
import mfuny.ggongal.admin.https.GGEncoder;
import mfuny.ggongal.admin.mygift.MyGiftService;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class HttpUserDecoder {
	
	@SuppressWarnings("unchecked")
	public String receiveDecode(String enStr) throws Exception {
		String deStr = GGDecoder.decode(enStr);
		
		JSONObject paramJson = (JSONObject)JSONValue.parse(deStr);
		JSONObject resultJson = new JSONObject();
		
		HttpUser user = new HttpUser();
		MyGiftService myGift = new MyGiftService();
		
		//현재 꽁 확인.
		if(paramJson.get("tag").equals("user_getGgong")) {
			resultJson = user.user_getGgong((String)paramJson.get("id"));
		} 
		
		//회원가입.
		else if( paramJson.get("tag").equals("user_insert")) {
			try {
				resultJson = user.user_insert2(paramJson);
				//resultJson.put("responseCode", result);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//로그인 체크.
		else if ( paramJson.get("tag").equals("user_login")) {
			try {
				resultJson = user.user_login(
						(String)paramJson.get("id"),
						(String)paramJson.get("pw"), 
						(String)paramJson.get("pnum"), 
						(String)paramJson.get("pname"), 
						(String)paramJson.get("pserial")
				);
			}catch(Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag") + " / " + paramJson.get("id"));
			}
		}
		
		//인증번호 요청.
		else if( paramJson.get("tag").equals("user_requestPin")) {
			try {
				resultJson.put("responseCode", user.requestPin((String)paramJson.get("pnum")));
			} catch(Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
			}
		}
		
		//인증번호 응답.
		else if( paramJson.get("tag").equals("user_responsePin")) {
			try {
				resultJson.put("responseCode", user.responsePin((String)paramJson.get("pnum"), (String)paramJson.get("pin")));
			} catch(Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
			}
		}
		
		//로그인 시 인증번호 확인.
		else if( paramJson.get("tag").equals("user_responsePinForLogin")) {
			try {
				resultJson = user.responsePinForLogin(paramJson);
			} catch(Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
				resultJson.put("error", e);
			}
		}
		
		//로그인 후 스페셜게임 검사건이 있는지 체크.
		else if ( paramJson.get("tag").equals("user_checkSpecial")) {
			try {
				resultJson = user.user_checkSpecial((String)paramJson.get("id"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//해당 유저의 출석 현황.
		else if( paramJson.get("tag").equals("user_getLogin_level")) {
			try {
				resultJson = user.user_getLogin_level((String)paramJson.get("id"));
			} catch(Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//아이디 존재여부 체크.
		else if( paramJson.get("tag").equals("user_idCheck")) {
			try {
				resultJson.put("responseCode", user.user_idCheck((String)paramJson.get("id")));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//이메일 존재여부 체크.
		else if( paramJson.get("tag").equals("user_emailCheck")) {
			try {
				resultJson = user.user_emailCheck((String)paramJson.get("email"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//아이디 찾기.
		else if ( paramJson.get("tag").equals("user_idFind")) {
			try {
				resultJson = user.idFind((String)paramJson.get("pnum"), (String)paramJson.get("email"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//비번 찾기.
		else if ( paramJson.get("tag").equals("user_pwFind")) {
			try {
				resultJson = user.pwFind((String)paramJson.get("id"), (String)paramJson.get("pnum"), (String)paramJson.get("email"));
			} catch (Exception e ) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//비번 체크.
		else if( paramJson.get("tag").equals("user_pwCheck")) {
			try {
				resultJson = user.user_pwCheck((String)paramJson.get("id"), (String)paramJson.get("pw"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//비번 변경.
		else if( paramJson.get("tag").equals("user_pwUpdate")) {
			try {
				resultJson = user.user_pwUpdate((String)paramJson.get("id"), (String)paramJson.get("pw"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//개인정보변경.
		else if( paramJson.get("tag").equals("user_infoUpdate")) {
			try {
				resultJson = user.user_infoUpdate(paramJson);
			} catch(Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//우편번호 검색.
		else if( paramJson.get("tag").equals("user_dongSearch")) {
			try {
				resultJson = user.user_dongSearch((String)paramJson.get("dond"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//배송지 입력.
		else if(paramJson.get("tag").equals("user_setAddress")) {
			try {
				resultJson = user.user_setAddress(paramJson);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//꽁 가감 처리.
		else if(paramJson.get("tag").equals("user_addGgong")) {
			try {
				resultJson = user.user_addGgong(paramJson);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		//꽁 가감 처리(암호화버전) vc-24.
		else if(paramJson.get("tag").equals("user_addGgong2")) {
			try {
				resultJson = user.user_addGgong2(paramJson);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//나의 꽁 내역 가져오기.
		else if( paramJson.get("tag").equals("user_getMyGgongList")) {
			try {
				resultJson = user.user_getMyGgongList(
						(String)paramJson.get("id"), 
						Integer.parseInt((String)paramJson.get("start")), 
						(String)paramJson.get("type")
				);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
			}
		}
		
		//꽁 랭킹(글로벌) 가져오기.
		else if( paramJson.get("tag").equals("user_getGgongRank")) {
			try {
				resultJson = user.user_getGgongRank(
						(String)paramJson.get("id"),
						Integer.parseInt((String)paramJson.get("start"))
				);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//경품 랭킹(글로벌) 가져오기.
		else if( paramJson.get("tag").equals("user_getGiftRank")) {
			try {
				resultJson = user.user_getGiftRank(
						(String)paramJson.get("id"), 
						Integer.parseInt((String)paramJson.get("start"))
				);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//유저 연락처에서 친구 리스트와 꽁랭킹 가져오기.
		else if(paramJson.get("tag").equals("user_getMyFriends")) {
			try {
				resultJson = user.user_getMyFriends((String)paramJson.get("pnums"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//유저 연락처에서 친구 리스트와 경품랭킹 가져오기.
		else if( paramJson.get("tag").equals("user_getMyFriendsGiftRank")) {
			try {
				resultJson = user.user_getMyFriendsGiftRank((String)paramJson.get("pnums"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//특정 번호의 내경품 정보.
		else if( paramJson.get("tag").equals("myGift_getMyGiftByNo")) {
			try {
				resultJson = myGift.getMyGiftByNo(Integer.parseInt((String)paramJson.get("mygift_no")));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//경품 받기로 했을 경우 추가 입력.
		else if( paramJson.get("tag").equals("myGift_setMyGift2")) {
			try {
				resultJson = myGift.setMyGift2(paramJson);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//나의 경품 당첨 리스트 가져오기.
		else if( paramJson.get("tag").equals("user_getMyGifts")) {
			try {
				resultJson = user.user_getMyGifts(
						(String)paramJson.get("id"), 
						Integer.parseInt((String)paramJson.get("start")), 
						Integer.parseInt((String)paramJson.get("type"))
				);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//특정 광고(경품게임) 의 당첨자 리스트 가져오기.
		else if( paramJson.get("tag").equals("user_getWinnerList")) {
			try {
				resultJson = user.user_getWinnerList(
						Integer.parseInt((String)paramJson.get("ad_no")), 
						(String)paramJson.get("days")
				);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//1:1 문의 등록.
		else if ( paramJson.get("tag").equals("user_regAsk")) {
			try {
				resultJson = user.user_regAsk(paramJson);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//나의 1:1 문의 내역.
		else if ( paramJson.get("tag").equals("user_getMyAskList")) {
			try {
				resultJson = user.user_getMyAskList((String)paramJson.get("id"), Integer.parseInt((String)paramJson.get("start")));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//사진 업로드.
		else if( paramJson.get("tag").equals("user_setMyPic")) {
			try {
				resultJson = user.user_setMyPic((String)paramJson.get("id"), (String)paramJson.get("mypic"));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//탈퇴 처리.
		else if ( paramJson.get("tag").equals("user_out")) {
			try {
				resultJson = user.user_out(paramJson);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		return GGEncoder.encodeForJson(resultJson);
	}
}
